Algorithmic correspondence and completeness in modal logic. I. The core algorithm SQEMA (Logical Methods in Computer Science, 2006)