restructured main code

changes:
- Code now saner