Optimized code works

However, array index conversion may be redundant. Can be improved.