Refactored a bit to clean up