Update almost working code, but there are some problems with ending programm. Please fix me.