add memory checking, bugfixing