took median out of while loop