add boundary checking to utility functions