Refactor to use better switch idiom