Ported some code to swift