Major refactoring and update