more functionization of code and commenting