use template, and fixed more code based on it