Added documentation, cleaned up code