+ some minor fixes and cleanup
+ added explicit error handling
+ fixed unsafe code