Fix orientation code; add debug option for debugging orientation code