Bug fixes and formatting of code