fixed endless loop bug