added refactored code from pr #235
expand tabs
added comments & documentation
expand tab to spaces
np to cnp
use six

get python 3  & python 2 compatibility