Add timer and a little JS refactoring