Fixes issue #27
Refactor a bit