More refactoring: cleanup, moving code around, parameterized main method

Signed-off-by: Martin Ankerl <martin.ankerl@gmail.com>