Clean up code using Util.Generate
Move vertex reading function out of the while loop and move all the logic to its own function

So we can use Util.Generate