Refactor to var name = function, fix memory leak