changed implementation to be more efficient