code optimization and finish