move with_model cleanup code to finally block