fixed query; refactoring