Initial work on the unit tests and initial bug fixes as a result.