rearange code to be logical