Added missing brackets to while block
Added checking for success