Code: Refactor and minor improvements.
Merge pull request #394 from am11/master

Code: Refactor and minor improvements