fixes #16, and more bugs