add UnitTests and fix bugs