reordered some of the functions to make for better reading