refactor hand made query