generalize circularize function