Replaced the huge ugly-ass switch statements by something better. Better performance as well.