More code cleanup and fixes