replaced ugly switch statement with better code