fix some undefined index errors and correctly preserve fragment