refactor step Replace temp with query