refactored masterLoop to avoid unnecessary variable juggling