Texture expression fix

Fixed a few of the issues mentioned by Esteban.

Signed-off-by: Marius Avram <marius.avram1309@gmail.com>
I would suggest moving all this code to a separate function and add unit tests for it. It isn't obvious what it does and whether it does it correctly all the times.