Partial rewrite to adhere to attempt to follow coding standards.