Refactor exercise code