Cleaned up, added some error handling