tweaking function to make it more general