rewrote the embedded function to be more idiomatic