Fixed a couple of bugs (again).
Fixed empty array foreach warnings and a couple of minor bugs while I was at it.