Fixed cleanup function