Refactored messages controller