fixed bug in algo update + refactored code