implemented a more efficient version