Refactored things to:

1. Remove #ifdef'd code that wasn't needed any more
2. Reduce code duplication
3. Make things tidier and more self-contained