code cleanup and other performance improvement