reformatted code, bug fixes