fix bugs and tests