Add unit tests and fix issues found