Major Code refactoring