rewritten on swift