Fixed minor error handling bugs, made test case more comprehensive.