Code cleanup and minor fixes