Small fixes and refactoring. Improved docs.