Remove dead code, make code more idiomatic, and improve style