reformatted code consistently and fixed possible logic errors