Replaced pow impl with possibly more efficient version.
Replace some loops using mutables with folds.