Made error handling a bit more robuts