Memfixes

Fixes for issues flagged by Dr.Memory and the MSVC static analysis tool, along with a few misc code hygiene improvements