Priority algorithm