Re-organisation of code, fix index emulation.

Signed-off-by: Bruno Lesieur <bruno.lesieur@gmail.com>
Fix emulated bug and re-organisation of code

Signed-off-by: Bruno Lesieur <bruno.lesieur@gmail.com>