Speed-up stage 5 more by buffering some variables.