First point to realise is that on lighting, a black wire need not necessarily be neutral, it may be a switched live. You're supposed to indicate this with tape or sleeving, but cowboys don't bother. Therefore, make no assumptions.
In general the best way to sort-out a tangle like this is to...