Changed implementation to be more pythonic