Added cleaning after error

If any error ocurred all allocated memory should be released
Program is waiting till created threads will exit and then release
memory and print error code