Fix/improve repair function