Minimize indexing and allocation in inner loop