Modified based upon Girolami's code