refactored to make it even more efficient