Small refactor of message loop