messagehandler refactoring + tests