Some refactoring. Fixes gh-2 and fixes gh-3