code cleanup and performance improvement