Refactor of Python library

Resubmit as requested.