I feel like there's a way to avoid having a nested if - else within and else.  consider using an if -elsif - else here instead rather than nesting in.