Added documentation, cleaned up code slightly.