Clean the code a bit and add better support for flipped sprites