code cleanup

use ‚switch/case‘ instead of ‚if/else‘ construct for state machine.