Code cleanup, added documentation