Miscellaneous cleanups, optimizations, and conversions to C99