Rewrite some code to use more pure Swift