added better commenting and made minor bug fixes