Major refactoring and renaming to make things clearer