redesign error handling, add test cases