Reorganised for better readability in article

Pushed the "lead" code path to the front of the code so that exception
handling and other functions are towards the end.