changed query and cleaned up structure