major enhancement, code refactoring for issue #297