rewrite, make more flexible