Implemented the changes suggested by @rossant. Tested in the notebook.