Fixing HPS bug & clean code