refactored message passing code