Close issue #133, fix segfault

Inner loop condition was using the outer loop index.