Added error checking, comments and documentation Now creates and uses temporary directory for each test