Code cleanup and other fixes