fix bugs and finish tests