Unit tests, fixes, and refactoring

A couple of commits to add some unit tests, and to fix some problems that were brought up. Also refactored for code standards and to make some methods testable