Improved main function