Check array length before indexing. Fixes #603
Merge pull request #604 from dazoe/fix-603

Check array length before indexing. Fixes #603