Refactor messages API