generalize the code