Tweaked to properly create directories as necessary.