Major refactor 

Splitted things to relatively logical places in order to maintain code readability.