code refactor and looping functionality

finally committing these changes after much too long a time