general code cleanup/factoring