Refactor query on messages box