Added enhanced error handling, some more test cases, commented functions