WIP refactoring following the modification for query