Improved resize, fixed memory leak