Fix unit test code and fix code issues