refactor code into a shared implementation