Fixed exception handling; wrote some unit tests (not completed)