This is a version of the code which stays as close as possible to Wanyi's dissertation version.