Abstracted switch statement into a function

Made the switch statement reusable and reduced the overall code.
Instead of repeating the same switch statement 4 or 5 times I created a
function that accepts 2 arguments allowing me to do what I was doing
before but with less code.