Attempt to make a quick fix (by dw2412 from #zarafa)
Cleaning up the code to make it really work (Andreas Brodowski).

Also fixing uninitialized variables