Improved error handling, thanks @MikeYV