Rework texture generating code, add texture grouping

A few lines, such as this one, are too long.