merge and refactor angular controller code