Replaced if/then witch switch/case

- replaced if/then with switch/case
- optimized code a bit