preliminary main script, problem in last loop