Tidier use of functions for calculation of orientation and fixed code formatting