adapted to pullrequest #20 from original repo