updated code to be more efficient