refactor shared functions