added refactored code from pr #235
expand tabs
added comments & documentation