big refactoring in main