Although this looks like it's working with the test file I used it still needs some stress testing!
Not tested yet!