Refactoring to avoid code duplication and optimize performance.
Merge pull request #298 from mageekguy/master

Refactoring to avoid code duplication and optimize performance.