redid some functions after some advice from Matt