ported to python3, added error handling