Reduce insane amount of code duplication and long lines

Change-Id: Ieae80c63e391cfb1af61841a6badd51b87d00cfe