Adding refacto of tutorial code from http://toastedware.com/?p=258 post