much code cleanup and profiling