Messy code changed to switch statements