prepare.cpp refactoring

Similar to extractor.cpp refactoring.