Fixes stupid bugs (plus some cleanup).

(Critical code should be compiled in *both* debugging and optimized modes.)