Code from PhD.