Fixes for clean compilation on MacOSX Mavericks. Also fixes some bugs discovered by Clang.