Fix "undefined behavior errors"