Cleanup enum handling and code duplication.
Merge pull request #65 from Dmitry-Me/reduceduplicationcleanupenumhandling

Cleanup enum handling and code duplication.