merge Dan's patches with small commenting/whitespace changes I'd made