Performance improvements, and bug fixes

This still needs tested, it's just some things my IDE pointed out, and I caught while reviewing some of the code.