forked fresh and patched for modern compile. very minor tweaks.