Refactored to better functional style