refactored further, I think I'm happy with this