Refactor message handling in IPC