Finally found the bug, working solver