put functions in a better order, add comments