Refactor to change filtering to post recursive query