refactor truncate code