small optimizations

- replaced misuse of count with forEach
- replaced nested if with switch