fixed dangerous while loops