Fix for issue #10 and refactoring