Some code cleanup and fixes.