Moved the mage copying into another function so the file.close calls can be deferred, thanks to cronos on Freenode#go-nuts for the suggestion
Make sure we only process directories