code optimization and restructure