Tidy up the code and make it Python 2/3 compliant:

- Proper indentation according to PEP 8
- Python 3 compliant octal numbers
- Python 2/3 compliant exception handling
- Remove redundant '== True' when testing conditions