refactor a bit the direction code