changed returned type to Option, but I don't see how to avoid the loop