Introduce setup_engine and remove code redundancy