fixing bugs and abstracting models and functions