refactor db queries