reworked algotithms to use contexts