added doctests and refactor the existing ones