Extracted logic to correct files and added tests.