Refactor algorithms