Various improvements to error handling + some other little fixes