cleanup and optimize