Made the editing code more robust