Update plugin.py

Fixed some bugs and made the code more user-proof