Do you think there might be a safer way to implement this function?