Renamed variables to make more sense and reordered them to be more in line with the function's purpose.