refactor to get rid of mid fucntion closes and returns in receive