improve tests and fix bugs