Converted to a more functional style