my first PAL algorithm