Refactored code

Refactored the code to use methods sendMessage and recvMessage