Some bugs and formatting fixed