Trying to fix undefined behaviours