moved large code block to where it belongs
formatting changed, identation
typo fixed