Refactoring code

Moving code out of main into separate files for better overview and
further development