a) Fixed a loop where an index could overflow and cause infinite loop; b) fixed two asserts