moving some code around to make more sense