Bug fixes and formatting