Bug fixes and reformatting