Code cleanup
Add error handling