clean up and improvements over issue #51