Fix 2 bugs that caused errors on output - accessing out of bounds indices.