including some comments by Tim Holy, rearrangement in the code