Finished with better error handling