Significant code refactoring