Refactored main loop, hopefully to allow it to working in a selcective receive mode