refactor assign_case function