rewrite to make more flexible