updated routine to be general