broke everything out to functions, some code cleanup