Refactored mean query