bug fixes, reformat code