cleaned code (use pattern match instead of if else, group variables...)