Fix segfault and infinite loop in TokenManager::prev