refactoring, moved a few general functions