massive code changes and bug fixes, some not entirely working or tested yet