Avoid IndexError and TypeError exceptions