restructure stuff and fix bugs