A more modular implementation