Fixed a bunch of issues pointed out by Ms2ger