Added support for empty directories and more error checking to see if files exist etc.