Refactor, cleanup

Hi, 
this is actually my very first pull-request, hoping everything is correct. 

I just performed some refactoring and cleanup to make the code more readable. 

Daniel