[BUGFIX] Improve display in UI
[FEATURE] Rearrange code to avoid code duplication and fixes some small bugs
Also adds lot of PHPDoc.