moving the query in one place. adding tests