added david's latest code