Python version of #59 and #66
Python version of #59 and #66