Reorgainize

Reorganized code and refactored some parts with the help of resharper
plugin to find redundancies/weird things.