fixes for latex, tweaks to algorithm
fixes for latex, tweaks to algorithm