fix bugs add some tests and coverage