making functions more pure and some cleanup