closes #13; some refactoring.