fixed bugs in cleaning up
add profile; to be reviewed.