Refactored to make things cleaner