Make main more modular and add optimization in compile option