Added Python 3 compatibility

This patch adds Python 3 compatibility, while preserving Python 2 compatibility.

Tested on Python 2.7 and 3.3.