More small optimizations; fixed g2 to only be computed once