add util.c, and move some functions from main.c to util.c