fix a few formatting bugs and simplify code a bit