Move script main to a separate function