Rewritten main function