Fixed problems with cleanup function