solution to code snippet