Major refactoring, address issue #12