added code using cache locality optimization