patch for bugs, indentation also cleaned up