JS refactoring (still WIP) and UI improvements