Several refactorings, see comment

Change-Id: Ibf03ca50589be23c18bfc68cc4b75d0b18f18421