Add unit tests and fix issues they found