Changed die to perror, refactoring pwd