Add more unit tests and fix problems