Take a look at the code I posted, understand it, and change your code to get rid of the gigantic switches.