refactor SQL backend