re-arranged data flow within main loop