Add error handling
Fixes and improvements