Add custom allocator support

centralizes memory management to be able to use custom allocators, minor whitespace fixing and cleanup.