Improve stack layout to use less space and improve performance