Add more error handling, remove Python 2.6ism