Refactorings

A few refactorings to tighten up the code.