use proper index, use clip function to simplify the code