added documentation, simplified some code