This functions seams to be overcomplicated especially the part with `default` return statement  and the code below the `switch`. @hakubo  please take a look and we could discuss it in the evening