Query steps refactoring and fixes