Code cleanup and improved documentation