optimized sequential code finished