Restructured the orientation fixing code.