refined code per Pete's email