incorporated code improvements from S.