Refactor API message handling code

No match.