Refactoring

Cleanup of the code. Look at commit messages for a better description.