Added some more error detection, changed to work rotated