RF: created generic optimization functions