big refactoring to change the import structure. Now needs debugging.
first working version after refactoring, may need some little debug