Added some invariantes and preconditions.