Complete logical refactoring

Pending testing.