Moved some functions to more logical places.