Adding unit tests and fixing errors