added logic to deal with accelerometer orientation