Fix speedup issues and out of bounds access