Cleaned CASE function