Added copying for safety, and special-cased length==1 case to avoid copy in the common case.