Moved functions closer to their usages