general refactorings; put code into separate functions