Added more checks to prevent conflicts when library is loaded multiple times