Fix undefined behaviour