Updated code to be more efficient