Fix terminal warning flood, and improve error detection. Thanks to vlj for help