A few structural changes to break down this script into easily manageable parts.