Now using filepath instead of os.
improved error handling, check that directory exists