refactor and add beginnings of function emitting