moved the setting up data structures to new function and restructured reciver code