Cleaner code, fixes #15, and fixes #11