Unit Test and more bug fixes