more robust error handling for libpng