Lots of code optimizations

I made many optimizations. Some are logical, some are just to make things shorter.