Not quite sure what is going wrong with your implementation. My guess would be you might have the wrong index specified somewhere or the wrong range.
I have a similar implementation here