Refactored some shared code