General code improvements and fixes.