typos fixed, minor attempts at optimization