Handy isWithinBounds function stolen from master branch