used std::advance to make functions more versatile