much refactoring, soon finished