Moving the contour generation to a function

Still uses pointers to avoid copying and memory overhead