Updated swift implementation