Performance improvements

Some things are done twice without the need to.
Also, switch is faster than if.