code cleanup and some inline documentation