Added unit tests and refactored some dynamic APEX