prepare function for contiguous optimization