updated logic to Listing 1.3