Improve code and testing for finally deduplication