fixed next function bug