Moved texture extraction stuff to a separate function