Code refactoring

Are you interested in pull requests like this? 

I've refectored the code a bit. Would continue to do so for some more things and then start contributing.