Refactoring

@mr-mig, @enginyoyen, please, check this out.
See comments to e10362c for some notes and explanation.