fix some severe bugs found with clang's help