fix indexing issue: do not increment i twice per loop
add range check to make sure, index does not get out of stack
Merge pull request #3 from hanslovsky/indexing

Thanks!