changed code to be more efficient