Avoid IndexError and TypeError exceptions
Add missing tests and improve some others