Source refactoring and cleanup