reordered code to make more sense