finished exercise 16 by using algorithm implementation from wikibooks