Re-implemented the repair function.