This function should probably only cache the characters that are being accessed to. This would hugely improve performances hopefully.