Cleaned up the code a bit and fixed some things I overlooked earlier.
There is still a problem with ordering the layers and they don't get
drawn in an order determined by their scale.