version two, added new functions and consolidated code