Try to refactor this method and extract independent logic into separate methods