Added version of the code that belongs to my PhD.