Correctly keep the file extension when copying files

This also includes fixes to the unittest for my last pull request.