Changed free to work recursively
Improved detach/attaching
Bugfix