some stylistic and logic changes