code cleanup, extract functions