fixes #6, #7, #8. and refactors.