Add exception trapper in pinning tab function.
Format and improve code.