Reorganized a bit Jeff's code