Improved error handling
Fixed tests