Fix #1 and #2
Improve error handling