perf improvements

from http://introcs.cs.princeton.edu/java/13flow/Factors.java.html
Merge branch 'working'