rearrange code to be more flexible