clean up function improved
add check before cleaning