Packaged the program into functions for ease of viewing and experimentation.