Id like to see a consistency check here before saving, but other than that this function looks good to me (not a thorough review though, sorry :(