Exception handling; improved cleanup option
added docstrings