Fixed bug in closing handles which caused Matlab to crash