Extensive cleanup and documentation