NOREF: Some of the logic was failing in the case statement. That is cleaned up now with proper syntax and quoting