Major refactoring and stabilization