fixed some undefined behavior