Fix build issue to not require header file hacks and fix memory leaks