Fixed device orientation calculation. Other assorted fixes.