Fix for issue #23 

some refactoring and fix issue #23