Cleaning up code and making objects fields in loops rather than local variables