fixes optimization bug

also contains further improvements and error corrections