Merged Kens changes with mine

Merged Kens latest update with the changes I previously made.  Slightly
modified to reflect the actual way the function works.