refactor; document code