split handler in two
refactored common functionality