moved cleanup function