close #15, improve error handling