Refactored to more flexible style