Refactored boundary checking to a separate class.