Fixed the sync function and upgraded the error handler