Catch index errors

This is to fix a problem I came across.