Reorder the code a bit and add some comments