Merging Dan's work and my fixes/additions