added another while loop, improved formatting