Some refactoring from tips from Lecture 7.