moved over code from dissertation, started commenting and refactoring