The CFME code base also has a big check for this which is a LOT more detailed.  Perhaps that code should be transplanted here.