memory leak removed, refactored