Refactoring and cleanup
issue #163