Refactor to move the main script into a library