Refactored query into a messages class