first despaghettization & bug fixes