updated: Fixed the code of the second phase so that the intermediate data are now stored in heap instead of stack :)