Remove race condition in send and receicve loops