added documentation, cleaned up code a little