Complete rewrite
Refactoring internal code