code cleanup and improved documentation