Cleanup Error handling and more pythonic