the original code belongs to Dan Grossman *http://www.cs.washington.edu/education/courses/cse341/11au/lec11.c