bugs fixed and formatting improved