Making algorithm work