Changed logic; changed to while loop