Better documentation, and slight refactoring