fixed heap / stack bug from earlier implementation