Major refactoring and internal changes