Refactor stats js and templates