fixes #9, #10, #11, and a few more bugs