fixed issue #7 and finalized code