added incremental counter to next to prevent infinite loop