Misc code improvements and fixes