Changed some return values, merged from upstream, and made a few fixes