Broken algorithm working good