code reformat, bug fixes