Refactor temp with query