Major refactor to be IPython friendly