Added comment “headers” for friendlier source reading

Also performed some minor reorganization of a few blocks of code, in order to group similar pieces of code together.