add boundary checking, fix import