refactor, and fixes #1