refactor as per stack overflow's advice.