refactor message handling to be more generic