fixed issue #32 and cleaned up some code