pseudo code altered slightly to reflect our discussion today