Refactored so common functions could be shared between scripts.