removed excess while loops