converted snippet to a function