Does the performance of this function matter? If so, it will sadly be faster to use a while loop.