updated code to closure pattern