fixed cardinality problem and unlocking files before they are overwritten