Refactor and compat

Make code compatible with Python 3. Refactor the `subscribe` function. For exact details, please see the commit messages and commit diffs.