fixed some bugs in auto-repair (manually cherry-picked from master)