fixed hor algoritmen :D:D and added some error handling