Reduce repitition in functions that are smimilar