main func rewrite