Here's a stab at the refactoring: #76