Refactored to be more flexible