Why do you set ret here? If it's != 0 you manually set it to -1.