Refactoring

First refactoring done at @PyStaDa.

Main difference is the use of ``logging`` and the logical parts of the module are split into seperate functions for easier reuse and/or testing.