Managed to switch to using stack instead of heap after fixing several annoying bugs.