Fix bugs found by coverity scan
Use switch-case instead of if