this function is super long. could we at least break the case/switch portion into a private member function for readability?