refactor ugly code