newer version!
updated for efficiency