bug fixes + reformat code