refactored and fixed issue #8