refactor  procedure in main to function