some code factoring