Misc fixes without a major API change

First of all, returning a string containing an error message in case of error is wrong, we have warnings and notices for that.
This is probably the most significant change here, the other ones being more about minor memleaks, copy/pasted code moved to a macro and coding standards.