Refactored code. Using function pointers which cleaned up the code nicely.