Refactor of least and greatest functions