code factoring, some speedups