Reordered functions to better follow explanation