cloned APM code for tinkering