changed code for handling while loops