Updated Swift implementation.