Re-written. The old solution was verbose and incomplete
Merge pull request #560 from abhinavc/master

Re-written