added fold and valid? functions