fix up bounds checking, remove some magic numbers