Fixed the receive function to handle partial data.