Modified the folder determination code to be more robust.