Refactored some things and set up the loop structure