Indentation and speed up by query refactoring