Major internal refactoring