Updated some functions to use foldr and map instead of recursion.