updated method to avoid memory issue