Bugs fixed and some refactoring (tested on Chrome & FF)