Cleanup

Replaces (rebased): https://github.com/Elgg/Elgg/pull/6616

I might have broken session handling after conflict with https://github.com/Elgg/Elgg/pull/6632, need to test it yet.