To Python 3

And some error handling.

I am not any good with Python (and programming in general), so just ignore this request if I messed something up.