Improved heap optimization