Cleaned up code, added docblocks