refactored code for retrieval queries