add uniqueFields option, reorganize code