Fix Issue #15 and refactor