Code documentation, cleanup and bugfix.