Refactoring in preparation for even more refactoring! Removed global variables and all that jazz!