integrating stoyan's code