Rewritten in Swift