Change ifs to switches, clean up code