break big functions to smaller ones

avoid code duplication