implemented debug function + seg fault fix