Rewritten most of functions to minimize mutation -> usertest