Added more unittest and fixed the bugs found