cleanup, fixed possible infinite loop