added a chain() function; modified some example code