Added unit test and changes from github PR comments.
use OrderedDict to preserve source order
pep8!