doing some pretty major refactoring and comenting of the code