some cleaning, added handling of duplicate filename
cleanup, error handling. everything seems to work