seperated query logic, updated template