Improved memory usage and fixed call stack bug