Major rework. Now works on rotated displays.