added collision detection with the boundaries