refactoring: extract function apply_msg_callback

in order to avoid nasty bugs in the future caused by copy and paste