simplified the code in ipython notebook