Clean up main and move the code to __main__.py

There are two TODOs in the code, I will fix soon. Also the GUI might be broken. But before I will ask someone to help me fixing it, I kindly ask to have a look over the new file in general.