Move functions and defintions for plugin-internal structs into separate file and fix memory leak
Remove unused functions and add some comments
Fix huge memory leak

Implement proper handling of imgstore reference counting and remove
several leaky memdup functions
Add new files to Makefile