Refactor app to avoid memoy leak