Remove while loop and clean up