refactor, internal functions