tightened up code for practical session 5.3