refactor and simplify results javascript