Fixed an thread synchronization issue which caused the plugin to crash
Also fixed some spacing issues with the code (cosmetics)