Large refactor aimed at increasing flexibility