Refactoring the code

Refactor some important portions of the code. Implements lpoo#114