code cleanup and optimization