fix bugs and missing code