Remove race condition in send and receive loops