Rewrite, added documentation.