[AM] refactoring + testing