Made the required changes.
Ok, I believe I fixed the issue, it should reflect the latest changes versus the master HEAD.
Made changes to fix the leak and to simplify the code. Also fixed formatting to match coding conventions.