Adding unit tests and fixing bugs found during unit testing.