Refine main code