Refactoring (modularization, cleanups)

This pull request is focused on the following things:

 * Moving more code out of libpromises to agents
 * Modularization (unexporting internal functions, moving functions between modules where appropriate)
 * Simplification of code (removing unused macros, functions, simplifying conditional compilation)
 * Generic cleanups (warning fixes)

Bugs affected: #1973, #2018
Bugs (to be) closed: #2014