refactor labels fitting code

extract some helper functions to make more obvious what's
code doing.