Tidying up code order and comments