fix a potential memory leak + code clean up