Add some unit tests and perform some bufgixes