improved overflow checking
integrated earlier changes