Messages api refactoring