Refactored key planning
Query implementation