Refactor SQL commands