Eliminate several needless checks and fix a few memory leaks