fixed memory leak, bugfix, refactoring