Moving script functionality into main() to allow for importing.