adding some code to be modified from La Sorte et al.