Query module bug, refactor