Handled exceptions in a better way