Code cleanup, added documentation.