Safe refactorings and cleanup