preliminary code for apo, glitching mostly fixed