Formalized from a couple of examples found on several help topics.   - solution simplified based on solution from pskopek on http://www.raspberrypi.org