Refactor confusing case statement