refactored code for lecture 5