Misc and portability fixes

Fixes cache purge, error handling and logging.

Also fixes Mac OS X build