improved formatting, comments, and some bug fixes