Refactoring

Applied some design patterns and best practice idioms, added a high
level documentation