optimised sequential version