Big Refactor

Use amd-defines, refactor tests with a factory, etc