FIX: refactor code, fix linewidth calculation