Rewrite init and fix bugs